package HomeWork.map;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 第一题:
 * 现已知世界杯举办的年份以及对应年份的冠军国家信息，
 * 数据如下：("1930","乌拉圭");("1934","意大利");("1938","意大利");("1950","乌拉圭");("1954","西德");("1958","巴西");("1962","巴西");("1966","英格兰");("1970","巴西");("1974","西得");("1978","阿根廷");("1982","意大利");("1986","阿根廷");("1990","西得");("1994","巴西");("1998","法国");("2002","巴西");("2006","意大利");("2010","西班牙");("2014","德国");("2018","法国");
 * 请编写程序，可以根据年份查询该年份的冠军国家，也可以根据国家，查询该国家获得世界杯冠军的所有年份。
 * 运行效果如下：请输入年份：20182018年的世界杯冠军是：法国请输入国家：巴西巴西获得世界杯的年份是：19621994197020021958------
 *              如果输入的年份和国家没有匹配，输出如下------请输入年份：20172017年没有举办世界杯
 *              请输入国家：中国 中国没有获得过世界杯冠军
 */
public class WorldCup {
    private static HashMap<String, String> map = new HashMap<>(Map.of("1930","乌拉圭","1934","意大利","1938","意大利","1950","乌拉圭",
            "1954","西德","1958","巴西","1962","巴西","1966","英格兰","1970","巴西","1974","西德"));
    static {
        map.putAll(Map.of("1978","阿根廷","1982","意大利","1986","阿根廷","1990","西德","1994","巴西",
                "1998","法国","2002","巴西","2006","意大利","2010","西班牙","2014","德国"));
        map.put("2018","法国");
    }
    private static final Scanner sc = new Scanner(System.in);

    public static void main (String[] args) {
        while (true) {
            System.out.println("请输入要查找的选项,1、按年份查找 2、按国家查找 3、退出:");
            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> yearSearch();
                case "2" -> countrySearch();
                case "3" -> {return;}
                default -> System.out.println("您输入的选项有问题，请重新输入");
            }
        }
    }

    private static void countrySearch() {
        while (true) {
            System.out.println("请输入要查找的国家，输入1退出查找 ：");
            String country = sc.nextLine();
            if ("1".equals(country)){
                return;
            }
            if (map.containsValue(country)){
                map.forEach((key,value) -> {
                    if (map.get(key).equals(country)){
                        System.out.print(key + " ");
                    }
                });
                System.out.println();
            } else {
                System.out.println(country + "没有获得过世界杯冠军");
            }
        }
    }

    private static void yearSearch() {
        while (true) {
            System.out.println("请输入要查找的年份，输入1退出查找 ：");
            String year = sc.nextLine();
            if ("1".equals(year)){
                return;
            }
            if (year.matches("\\d{4}")){
                if (map.containsKey(year)){
                    System.out.println(map.get(year));
                } else {
                    System.out.println(year + "年没有举办世界杯");
                }
            } else {
                System.out.println("您输入的年份有误，请重新输入");
            }
        }

    }
}
